package com.lh.tams.controller.system;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lh.tams.controller.qo.PersonQo;
import com.lh.tams.domain.TamsDept;
import com.lh.tams.domain.TamsPerson;
import com.lh.tams.domain.TamsRole;
import com.lh.tams.service.system.PersonService;
import com.lh.tams.util.Constants;
import com.lh.tams.util.DataGridModel;
import com.lh.tams.util.ID;
import com.lh.tams.util.Page;
import com.lh.tams.util.Status;
import com.lh.tams.util.diy.Json;

/**
 * 警员管理controller
 * @author liulin
 * @version 2012-05-19
 */
@RequestMapping("/person")
@Controller
public class PersonController {
	private PersonService personService;
	
	public PersonService getPersonService() {
		return personService;
	}
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}
	
	@RequestMapping("/queryPerson.adm")
	@ResponseBody
	public TamsPerson queryPerson(String userName){
		return personService.query(userName);
	}
	
	@RequestMapping("/modify.adm")
	@ResponseBody
	public Json modify(String personUsername,String personName,String sex,String phone,String address,String remark){
		personService.modify(personUsername,personName, sex, phone, address, remark);
		return new Json();
	}
	
	@RequestMapping("/modifyPwd.adm")
	@ResponseBody
	public Json modifyPwd(String personUsername,String oldPwd,String newPwd1,String newPwd2){
		personService.modify(personUsername,oldPwd,newPwd1,newPwd2);
		return new Json();
	}
	
	@RequestMapping("/query.adm")
	@ResponseBody
	public Page query(DataGridModel dgm,PersonQo personQo){
		return personService.query(dgm, personQo);
	}
	
	@RequestMapping("/add.adm")
	@ResponseBody
	public Json add(TamsPerson person,long roleId,long deptId){
		//生成dept对象
		TamsDept dept = new TamsDept(deptId);
		//生成role对象
		TamsRole role = new TamsRole(roleId);
		
		person.setIdBgt(ID.next());
		person.setTamsDept(dept);
		person.setTamsRole(role);
		person.setStatus(Status.STATUS_NORMAL);
		
		person.setPersonPassword(Constants.PASSWORD);
		personService.add(person);
		Json json = new Json();
		json.setMsg("新增成功");
		return json;
	}
	
	@RequestMapping("/update.adm")
	@ResponseBody
	public Json update(TamsPerson person,long roleId,long deptId){
		//生成dept对象
		TamsDept dept = new TamsDept(deptId);
		//生成role对象
		TamsRole role = new TamsRole(roleId);
		
		person.setTamsDept(dept);
		person.setTamsRole(role);
		
		//设置密码
		person.setPersonPassword(Constants.PASSWORD);
		
		person.setStatus(Status.STATUS_NORMAL);
		
		personService.update(person);
		Json json = new Json();
		json.setMsg("修改成功");
		return json;
	}
	
	@RequestMapping("/resetPwd.adm")
	@ResponseBody
	public Json resetPwd(long[] id){
		personService.resetPwd(id);
		return new Json();
	}
	
	@RequestMapping("/del.adm")
	@ResponseBody
	public Json delete(long[] id){
		personService.delete(id);
		return new Json();
	}
}
